package channel;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;


import stream.Stream;
import task.TaskState;

public class ChannelTime implements Channel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	static public final int TYPE = DATE_TIME;

	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		
		System.out.println(calendar.get(Calendar.YEAR));
		System.out.println(calendar.get(Calendar.MONTH) + 1);
		System.out.println(calendar.get(Calendar.DATE));
		System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println(calendar.get(Calendar.MINUTE));
	}
	

	static public final int EVERY_HOUR = 0;
	static public final int EVERY_DAY = 1;
	static public final int EVERY_MONTH = 2;
	static public final int EVERY_YEAR = 3;

	static public final int SAVE_TIME_INTERVAL = 
						TaskState.TIME_INTERVAL * 100;
	
	private Date date = new Date();
	private int frequency;
	
	
	@Override
	public int THIS() {
		
		Calendar calendarOld = Calendar.getInstance();
		Calendar calendarNow = Calendar.getInstance();
		
		calendarOld.setTime(date);
		calendarNow.setTime(new Date());
		

		calendarOld.set(Calendar.YEAR, 0);
		calendarNow.set(Calendar.YEAR, 0);
		
		if ( frequency != EVERY_YEAR ) {

			calendarOld.set(Calendar.MONTH, 0);
			calendarNow.set(Calendar.MONTH, 0);
			
		} else if ( frequency != EVERY_MONTH ) {
			
			calendarOld.set(Calendar.DATE, 0);
			calendarNow.set(Calendar.DATE, 0);
			
		} else if ( frequency != EVERY_DAY ) {
			
			calendarOld.set(Calendar.HOUR_OF_DAY, 0);
			calendarNow.set(Calendar.HOUR_OF_DAY, 0);
		}

		
		long deltaTime = calendarNow.getTimeInMillis() - 
							calendarOld.getTimeInMillis();
		
		if ( deltaTime < SAVE_TIME_INTERVAL &&
			-deltaTime < SAVE_TIME_INTERVAL ) {
			return 1;
		}
		else
			return 0;
	}

	@Override
	public void THAT() {
		// NOTHING
	}
	
	public void update() {
		
	}
	
	public int getType() {
		return TYPE;
	}
	
	public void setfrequency(int fre){
		this.frequency = fre;
	}
	
	public void setDate(Date d)
	{
		this.date = d;
	}
	
	public int getfrequency(){
		return this.frequency;
	}
	
	public Date getDate(){
		return this.date;
	}
	
	public InputStream toStream() {
		return Stream.getStream(this);
	}

}
